package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.service.FansService;
import com.zzgele.qmzb.api.service.RoomService;
import com.zzgele.qmzb.api.service.UserVService;
import com.zzgele.qmzb.api.service.VideoListService;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Martin on 2017/07/13.
 */
@RestController
@RequestMapping("/public")
public class publicController {
    private final Logger logger = LoggerFactory.getLogger(publicController.class);

    @DubboConsumer
    private UserVService userVService;
    @DubboConsumer
    private FansService fansService;
    @DubboConsumer
    private RoomService roomService;
    @DubboConsumer
    private VideoListService videoListService;

    @RequestMapping("/health")
    public Result health(  String channelName,String pushDomain,String channelType,String pushId,
                           String pullId,String channelState) {
        logger.debug("String channelName {},String pushDomain  {},String channelType {},String pushId {},\n" +
                "    String pullId {},String channelState {}",channelName,pushDomain,channelType,pushId,pullId,channelState);

//        CheckStatus status= JSON.parseObject(t, CheckStatus.class);

        if(channelState!=null) {
            if(!"1".equals(channelState))
                roomService.closeLivingRoom(pullId, channelState);
            logger.debug("直播间:{} status:{}",pullId,channelState);
        }
        logger.debug("public->health");

        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/my_info")
    public Result my_info(Long login_uid) {
        Integer status=1;
        HashMap hashMap=null;
        hashMap = roomService.getMyInfo(login_uid);
        if (hashMap==null){
            status=0;
            hashMap =new HashMap();
        }
        PageUtil.startPage(1, 9);
        HashMap map = new HashMap();
        map.put("room_status", 1);
        map.put("order", "u.hot_desc DESC,room.id ASC");
        List<HashMap> roomList = roomService.getList(map);
        List<HashMap> roomList2=new ArrayList<>();
        for(HashMap temp :roomList){
            HashMap temp1=new HashMap();
            temp1.put("uid",temp.get("uid")) ;
            temp1.put("icon",temp.get("icon")) ;
            roomList2.add(temp1);
        }

        //获取用户+v信息
        HashMap uservMap = userVService.getMyVInfo(login_uid);
        if(uservMap != null && uservMap.size() > 0) {
            hashMap.put("v_des", uservMap.get("v_des")); //+v描述信息
            hashMap.put("v_img_url", uservMap.get("v_img_url")); //+v图片地址
        }

        hashMap.put("status",status);
        hashMap.put("room_list",roomList2);
        return ResultGenerator.genSuccessResult(hashMap);
    }


    @RequestMapping("/share_video")
    public Result share_video(Long vid) {
        HashMap map =null;
        map = new HashMap();
        map.put("vid",vid);
        List<HashMap>  list = videoListService.recomendList(map);
        HashMap hashMap = new HashMap();
        int status=0;
        if(list.size()>0){
            for(HashMap item:list){
                String s=(String)item.get("file_name");
                item.put("url",OssController.Bucket.get("qmwlzb-one")+s);
            }
            hashMap.put("uid", list.get(0).get("uid"));
            hashMap.put("maxicon", list.get(0).get("icon"));
            hashMap.put("nickName", list.get(0).get("nickName"));
            hashMap.put("url", list.get(0).get("url"));
            hashMap.put("logo", list.get(0).get("logo"));
            hashMap.put("count",Math.random()*9000+1000);
            status=1;
        }
        map = new HashMap();
        PageUtil.startPage(1, 9);
        List<HashMap> videoList = videoListService.recomendList(map);
        List<HashMap> videoList2=new ArrayList<>();
        for(HashMap temp :videoList){
            HashMap temp1=new HashMap();
            temp1.put("vid",temp.get("id")) ;
            temp1.put("icon",temp.get("icon")) ;
            videoList2.add(temp1);
        }
        hashMap.put("video_list",videoList2);
        hashMap.put("status",status);
        return ResultGenerator.genSuccessResult(hashMap);
    }

}
